package com.sbw.springcloud.consumer.ribbon.demo.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: sangbowei
 * @Description: 断路器
 * @Date: Created in  17:50 2017\12\28 0028
 **/
@Service
public class RibbonService {

    Logger logger = LoggerFactory.getLogger(RibbonService.class);

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "discoveryClientFallback")
    public String ribbonDiscoveryClient() {
        logger.info("ribbonDiscoveryClient start");
        return restTemplate.getForObject("http://eureka-client/discovery_client",String.class);
    }
    private String discoveryClientFallback(){
        logger.info("discoveryClientFallback start");
        return "Sorry , this is ribbonDiscoveryClient method default value! ";
    }

    @HystrixCommand(fallbackMethod = "helloFallback")
    public String ribbonHello() {
        logger.info("ribbonHello start");
        return restTemplate.getForObject("http://eureka-client/hello",String.class);
    }
    private String helloFallback(){
        logger.info("helloFallback start");
        return "Sorry , this is ribbonHello method default value! ";
    }

    @HystrixCommand(fallbackMethod = "helloDelayFallback")
    public String ribbonHelloDelay() {
        logger.info("ribbonHelloDelay start");
        return restTemplate.getForObject("http://eureka-client/hello_delay",String.class);
    }
    private String helloDelayFallback(){
        logger.info("helloDelayFallback start");
        return "Sorry , this is ribbonHelloDelay method default value! ";
    }

    @HystrixCommand(fallbackMethod = "helloParamsFallback")
    public String ribbonHelloParams(String name) {
        logger.info("ribbonHelloParams start");
        return restTemplate.getForObject("http://eureka-client/hello_params?name="+name,String.class);
    }
    private String helloParamsFallback(String name){
        logger.info("helloParamsFallback start");
        return "Sorry , this is ribbonHelloParams method default value! name is "+name;
    }

    @HystrixCommand(fallbackMethod = "helloClientFallback")
    public String ribbonHelloClient() {
        logger.info("ribbonHelloClient start");
        return restTemplate.getForObject("http://eureka-client/hello_client",String.class);
    }
    private String helloClientFallback(){
        logger.info("helloClientFallback start");
        return "Sorry , this is ribbonHelloClient method default value! ";
    }

}